package edu.cuit.avatar.parallel;

import java.util.Arrays;
import java.util.Random;

/**
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/7/18 9:44
 */
public class MergeSortTest {
    public static void main(String[] args) {
        final int SIZE = 8_000_000;
        int[] list1 = new int[SIZE];
        int[] list2 = new int[SIZE];
        var random = new Random();

        //对两个数组进行初始化
//        for (var i = 0; i < SIZE; i++)
//            list1[i] = list2[i] = (int)(Math.random() * 10000000);
        //并行初始化
        //此处应该避免使用Math.random(),因为它是同步执行的，不可以并行执行
        Arrays.parallelSetAll(list1, i -> random.nextInt(10000000));
        Arrays.parallelSetAll(list2, i -> random.nextInt(10000000));


        var begin = System.currentTimeMillis();
        MergeSort.sort(list1);
        var end = System.currentTimeMillis();
        System.out.println("普通版归并排序:" + (end - begin) + "ms");

        begin = System.currentTimeMillis();
        ParallelMergeSort.sort(list2);
        end = System.currentTimeMillis();
        System.out.println("并行版归并排序:" + (end - begin) + "ms");

        System.out.println(Arrays.toString(Arrays.copyOf(list2, 20)));
    }
}
